package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.StringValueExp;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mvc.entity.Banji;
import com.situ.mvc.entity.Course;
import com.situ.mvc.entity.PageBean;
import com.situ.mvc.service.IBanjiService;
import com.situ.mvc.service.ICourseService;
import com.situ.mvc.service.impl.BanjiServiceImpl;
import com.situ.mvc.service.impl.CourseServiceImpl;

@Controller
@RequestMapping(value="/banji")
public class BanjiController {
	private IBanjiService banjiService = new BanjiServiceImpl();
	private ICourseService courseService = new CourseServiceImpl();
	 
	 /*分页*/
	@RequestMapping(value="/pageList")
	private String pageList(Integer pageNo,Integer pageSize,Model model) {
		 if (pageNo == null) {
			pageNo = 1;
		}
		 if (pageSize == null) {
			pageSize = 3;
		}
		//封装为pageBean，调用业务逻辑
		PageBean<Map<String, Object>> pageBean = banjiService.getPagBean(pageNo,pageSize);
		System.out.println(pageBean);
		
		model.addAttribute("pageBean", pageBean);//展示的分页
		return "banji/banji_list";
	}
	 
	 /*多表查询，展示课程信息*/ 
	 @RequestMapping(value="/getAddPage")
	 private String getAddPage(Model model) {
		 List<Course> list = courseService.list();
		 model.addAttribute("list", list);
		 return "banji/banji_add";
	 }
	 
	 /*删除*/
	 @RequestMapping(value="/deleteById")
	 @ResponseBody
	 private Boolean deleteById(Integer id,Model model){
		boolean result =  banjiService.deleteById(id);
		System.out.println(result ? "成功" : "失败");
		return result;
	 }
	 
	 /*全部删除*/
	 @RequestMapping(value="/deleteAll")
	 @ResponseBody
	 private Boolean deleteAll(String[] selectIds) {
		 boolean result = banjiService.deleteAll(selectIds);
		 System.out.println(result ? "成功" : "失败");
		 return result;
	 } 
	
	 
	 //查出课程信息，转发到添加页面
	 @RequestMapping(value="getInsertPage")
	 protected String getInsertPage(Model model) {
		List<Course> list = courseService.list();
		model.addAttribute("list", list);
		return "banji/banji_insert";
	}
	 
	 /*添加*/
	 @RequestMapping(value="/insert")
	 @ResponseBody
	private Boolean insert(String name,Integer people,String teatch,Integer courseId) {
		Banji banji = new Banji(name, people, teatch, courseId);
		//service方法
		boolean resule = banjiService.insert(banji);
		System.out.println(resule ? "成功" : "失败");
		//重定向
		return resule;
	}
	
	 /*修改前，展示页面*/
	@RequestMapping(value="/toUpdate")
	private String toUpdate(Integer id,Model model) {
		Banji banji = banjiService.findById(id);
		//显示课程列表
		List<Course> list = courseService.list();
		model.addAttribute("list", list);
		/*转发*/
		model.addAttribute("banji", banji);
		return "banji/banji_update";
	}
	 
	/*修改数据*/
	@RequestMapping(value="/update")
	@ResponseBody
	private Boolean update(Integer id,String name,Integer people,String teatch,Integer courseId) {
		
		Banji banji = new Banji(id, name, people, teatch, courseId);
		/*service方法*/
		boolean result = banjiService.update(banji);
		System.out.println(result ? "成功" : "失败");
		/*重定向*/
		return result;
	} 
	
	/*ajax请求名字不重复 */
	@RequestMapping(value="/checkName")
	private void checkName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		boolean isExist =  banjiService.checkName(name);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"isExist\": "+isExist+"}");
	}
}

